<?php

    namespace Admin\Controller;

    use Common\Controller\AdminbaseController;

    class ExamineController extends AdminbaseController {

        protected $m_logistics;
        protected $m_recharge;
        protected $m_examine;

        public $status_label = array(
            '0'=>'未执行',
            '1'=>'待审核',
            '2'=>'通过',
            '3'=>'不通过'    
        );

        public $type_label = array(
            'logistics_recharge'=>'上级物流公司充值',
            'sale_manager_recharge'=>'上级消费机构充值',
        );

        function _initialize() {
            parent::_initialize();
            $this->m_logistics= D("Admin/Logistics");
            $this->m_recharge= D("Admin/Recharge");
            $this->m_examine= D("Admin/Examine");
        }

        /**
         * 待审核列表
         */
        public function index(){
            // dump($this->user_model);

            $user_model = $this->user_model;
            $admin_id = session('ADMIN_ID');

            $where_str = "(e.role_id = $user_model OR e.admin_id = $admin_id) AND e.status > 0";

            $count = $this->m_examine->alias("e")->where($where_str)->count();
            $page = $this->page($count, 15);

            $result = $this->m_examine->alias('e')
                ->field('e.id, e.name ,e.step ,e.role_id ,e.type ,e.relate_id ,e.admin_id ,e.desc ,e.status ,e.create_time ,e.update_time, r.number ,r.before_balance ,r.price ,r.general_price ,r.after_balance ,r.status as r_status, r.model, l.logistics_name, l.balance as l_balance, s.sale_manage_name, s.balance as s_balance, u.user_nicename')
                ->join("bdt_recharge r ON (r.id = e.relate_id)")
                ->join("bdt_logistics l ON (l.id = r.rid and r.model = '1')","LEFT")
                ->join("bdt_sale_manager s ON (s.id = r.rid and r.model = '3')","LEFT")
                ->join("bdt_users u ON (u.id = e.admin_id)","LEFT")
                ->where($where_str)
                ->limit($page->firstRow , $page->listRows)
                ->order("e.create_time DESC")
                ->select();


            if($result) foreach ($result as $_key => &$_row) {
                $_row['status_label'] = $this->status_label[$_row['status']];
                if ($_row['model'] == 1) {
                    $_row['company_name'] = $_row['logistics_name'];
                    $_row['company_balance'] = $_row['l_balance'];
                    $_row['company_type'] = '上级物流公司';
                }elseif ($_row['model'] == 3) {
                    $_row['company_name'] = $_row['sale_manage_name'];
                    $_row['company_balance'] = $_row['s_balance'];
                    $_row['company_type'] = '上级消费机构';
                }
            }

            $logistics_list = $this->m_logistics->get_list();
            $this->assign("logistics_list",$logistics_list);
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$result);
            $this->display();
        }

        // 添加物流公司
        public function detail(){
            $id = I("get.id",0,'intval');
            // $logistics_list = $this->m_logistics->get_list();
            // $this->assign("logistics_list",$logistics_list);
            if(!empty($id)){

                $item = $this->m_examine->alias('e')
                    ->field('e.id, e.name ,e.step ,e.role_id ,e.type ,e.relate_id ,e.admin_id ,e.desc ,e.status ,e.create_time ,e.update_time, r.number ,r.before_balance ,r.price ,r.general_price ,r.after_balance ,r.status as r_status, r.model, l.logistics_name, l.balance as l_balance, s.sale_manage_name, s.balance as s_balance, u.user_nicename')
                    ->join("bdt_recharge r ON (r.id = e.relate_id)")
                    ->join("bdt_logistics l ON (l.id = r.rid and r.model = '1')","LEFT")
                    ->join("bdt_sale_manager s ON (s.id = r.rid and r.model = '3')","LEFT")
                    ->join("bdt_users u ON (u.id = e.admin_id)","LEFT")
                    ->where(array('e.id'=>$id))
                    ->find();

                $examine_list = $this->m_examine->alias('e')
                    ->field('e.*, u.user_nicename')
                    ->join("bdt_users u ON (u.id = e.admin_id)","LEFT")
                    ->where(array('relate_id'=>$item['relate_id']))->select();

                $item['status_label'] = $this->status_label[$item['status']];
                if ($item['model'] == 1) {
                    $item['company_name'] = $item['logistics_name'];
                    $item['company_balance'] = $item['l_balance'];
                    $item['company_type'] = '上级物流公司';
                }elseif ($item['model'] == 3) {
                    $item['company_name'] = $item['sale_manage_name'];
                    $item['company_balance'] = $item['s_balance'];
                    $item['company_type'] = '上级消费机构';
                }

                if($examine_list) foreach ($examine_list as $_key => &$_row) {
                    $_row['status_label'] = $this->status_label[$_row['status']];
                    $_row['type_label'] = $this->type_label[$_row['type']];
                    $_row['company_name'] = $item['company_name'];
                    $_row['company_balance'] = $item['company_balance'];
                    $_row['price'] = $item['price'];
                    $_row['after_balance'] = $item['after_balance'];
                    $_row['company_type'] = $item['company_type'];
                }

                if (I('s_download') == 1) {
                    $export_title = array(
                        'step' => '审核步骤',
                        'name' => '步骤名称',
                        'type_label' => '审核类型',
                        'user_nicename' => '操作用户',
                        'desc' => '审核描述',
                        'status_label' => '审核状态',
                        'company_name' => '公司名称',
                        'price' => '充值金额',
                        'after_balance' => '充值后余额',
                        'company_type' => '公司类型',
                        'update_time' => '审核时间',
                    );

                    $excel = new \Common\Lib\excel();
                    $excel->export_data($item['company_name'].'-'.$item['type_label'].'审核记录', $export_title, $examine_list);
                    exit;
                }

                $this->assign("item",$item);
                $this->assign("examine_list",$examine_list);
            }

            $this->display();
        }

        /**
         * 审核
         */
        public function detail_examine(){
            $examine_id = I("request.examine_id");
            $desc = I("request.desc");

            if(empty($examine_id)){
                $this->error("参数为空！");
            }

            $status = I("request.status");
            if(!in_array($status,array(2,3))){
                $this->error("参数有误！");
            }

            $result = $this->m_examine->do_examine($examine_id, $status, $desc);

            if($result > 0 ){
                $this->success("操作成功！");
            }elseif ($result == -1) {
                $this->error("未找到审核单！");
            }elseif ($result == -2) {
                $this->error("此审核单已审核过！");
            }elseif ($result == -3) {
                $this->error("操作失败，状态出错！");
            }elseif ($result == -4) {
                $this->error("当前审核单保存失败！");
            }elseif ($result == -5) {
                $this->error("下一步审核开启失败！");
            }elseif ($result == -6) {
                $this->error("充值单状态更新失败！");
            }else{
                $this->error("操作失败！".$result);
            }
        }
    }